import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit';
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
//切片模块
import subjectReducer from './slice/subject';
import userReducer from './slice/user';
import todolistRecuer from './slice/todolist';

// 类型定义
export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
export type AppThunk<ReturnType = void> = ThunkAction<
  ReturnType,
  RootState,
  unknown,
  Action<string>
>;

export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

//切片原理
export const store = configureStore({
  //指定reducer
  reducer: {
    //按模块管理各个切片导出的reducer
    subject: subjectReducer,
    user: userReducer,
    todolist: todolistRecuer,
  },
});
